﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Data.Models;

namespace Parafarmacia.UserControls
{
    public partial class ProductList : System.Web.UI.UserControl
    {
        public enum Tipo { Producto, Servicio }
        public Tipo _tipo;

        public string TipoStr;
        PagedDataSource pg = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        IQueryable<tblProducto> ProductResult;
        /// <summary>
        /// search products by optionSelected
        /// </summary>
        private void Search()
        {
            DataListProducto.DataSourceID = null;
            dbParafarmaciaContext db = new dbParafarmaciaContext();

            ProductResult = from p in db.tblProductos where p.Tipo == TipoStr select p;
            int SubSearchByID = -1;
            try
            {
                SubSearchByID = Convert.ToInt16(Marca_CategoriaFilterBy.SelectedValue);
            }
            catch
            {
                //catch
            }

            switch (this.ListFilterBy.SelectedValue)
            {
                case "Contenido":
                    ProductResult = from p in db.tblProductos where p.Tipo == TipoStr && (p.NombreProducto.Contains(txtSearch.Text) || p.Descripcion.Contains(txtSearch.Text)) select p;
                    break;

                case "Marca":
                    ProductResult = from p in db.tblProductos where p.IdMarca == SubSearchByID && p.Tipo == TipoStr && (p.NombreProducto.Contains(txtSearch.Text) || p.Descripcion.Contains(txtSearch.Text)) select p;
                    break;

                case "Categoria":
                    int CategoriaID = Convert.ToInt16(Marca_CategoriaFilterBy.SelectedValue);
                    ProductResult = from p in db.tblProductos where p.IdCategoria == CategoriaID && p.Tipo == TipoStr && (p.NombreProducto.Contains(txtSearch.Text) || p.Descripcion.Contains(txtSearch.Text)) select p;

                    break;
            }

            //Order data
            switch (OrderByList.SelectedValue)
            {
                case "Nombre":
                    ProductResult.OrderBy(o => o.NombreProducto);
                    break;

                case "PrecioMenor":
                    ProductResult.OrderBy(o => o.Precio);
                    break;

                case "PrecioMayor":
                    ProductResult.OrderByDescending(o => o.Precio);
                    break;
            }

            GetItems(pg);

            if (ProductResult.Count() == 0)
            {
                lblInfoPage.Visible = false;
                SiguienteLink.Visible = false;
                AnteriorLink.Visible = false;
                lblInfo.Visible = true;
            }
        }

        /// <summary>
        /// Display data depending on pageIndex selected.
        /// </summary>
        /// <param name="pg"></param>
        private void GetItems(PagedDataSource pg)
        {
            //Paging 
            pg.AllowPaging = true;
            pg.DataSource = ProductResult.ToList();
            pg.PageSize = 6;
            int currentpage;
            if (Session["cp"] != null)
            {
                currentpage = (int)(Session["cp"]);
            }
            else
            {
                currentpage = 0;
            }
            pg.CurrentPageIndex = currentpage;

            // Bind data
            DataListProducto.DataSource = pg;
            DataListProducto.DataBind();

            this.lblInfoPage.Visible = true;
            this.lblInfoPage.Text = String.Format("Página {0} de {1}", (pg.CurrentPageIndex + 1), pg.PageCount);
        }

        /// <summary>
        /// Previous page
        /// </summary>
        private void Previous()
        {
            int currentpage;
            if (!pg.IsFirstPage)
            {
                if (Session["cp"] != null)
                {
                    currentpage = (int)(Session["cp"]);
                }
                else
                {
                    currentpage = 0;
                }
                currentpage -= 1;
                Session.Add("cp", currentpage);
                GetItems(pg);
            }
        }

        //Next Page
        private void Next()
        {
            int currentpage;
            if (!pg.IsLastPage)
            {
                if (Session["cp"] != null)
                {
                    currentpage = (int)(Session["cp"]);
                }
                else
                {
                    currentpage = 0;
                }

                currentpage += 1;
                Session.Add("cp", currentpage);
                GetItems(pg);
            }


        }

        protected void DataListProducto_Load(object sender, EventArgs e)
        {
            Search();

            lblInfo.Text = "No se han encontrado " + TipoStr.ToLower() + "s.";

            if (!IsPostBack)
            {
                //DataListProducto.DataSourceID = "LinqDataSource";
                HabilitarControles();
                lblInfo.Visible = false;
                lblInfoPage.Visible = true;

                SiguienteLink.Visible = true;
                AnteriorLink.Visible = true;
            }

            lblInfo.Visible = false;

            SiguienteLink.Visible = true;
            AnteriorLink.Visible = true;
        }


        protected void ListFilterBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            HabilitarControles();
        }

        /// <summary>
        /// Habilita controles 
        /// </summary>
        private void HabilitarControles()
        {

            dbParafarmaciaContext db = new dbParafarmaciaContext();

            switch (ListFilterBy.SelectedValue)
            {
                case "Marca":

                    Marca_CategoriaFilterBy.Visible = true;
                    Marca_CategoriaFilterBy.DataBind();

                    break;

                case "Categoria":
                    Marca_CategoriaFilterBy.Visible = true;
                    break;

                default:
                    Marca_CategoriaFilterBy.Visible = false;

                    break;
            }
        }

        protected void AnteriorLink_Click(object sender, EventArgs e)
        {
            Previous();
        }

        protected void SiguienteLink_Click(object sender, EventArgs e)
        {
            Next();
        }
    }
}